********************************************************************************
qui: use localemployment${version}.dta, clear
qui: merge 1:1 idtime using nationalemployment.dta
qui: drop _merge
qui: gen natpeak = .
qui: replace natpeak = 1 if natempl_point == 1
qui: gen id_nat = sum(natpeak)
qui: replace id_nat = . if natpeak == .
qui: sum id_nat							
qui: local maxC = `r(max)'
forv ii = 1(1)$N {
qui: gen cyclesync`ii' = .
forv c = 1(1)`maxC'{
qui: tsset idtime
qui: replace cyclesync`ii' = `c' if employment_point`ii' == 1 & (id_nat== `c'|l1.id_nat == `c'|l2.id_nat == `c'|l3.id_nat == `c'|l4.id_nat == `c'|f1.id_nat == `c'|f2.id_nat == `c'|f3.id_nat == `c'|f4.id_nat == `c')
}
}
qui: keep idtime cyclesync*
qui: reshape long cyclesync, i(idtime) j(amr)
qui: duplicates drop
qui: keep if cyclesync != .
qui: save cyclesync${version}.dta, replace
********************************************************************************
forv n = 1(1)$N {
qui: use localemployment${version}.dta, clear 	
qui: keep employment_point`n' cycle_linear_${var_y}`n' cycle_hp_${var_y}`n' idtime
* Keep only peaks and troughs:
qui: keep if employment_point`n'!=0		
* Dummy for whether peak or trough:
qui: gen peak = .
qui: replace peak = 1 if employment_point`n'==1
qui: gen trough = .
qui: replace trough = 1 if employment_point`n'==-1
* Index local business cycles:
qui: tsset idtime
qui: gen id_lbc = sum(peak)
qui: gen trough_date = idtime*trough
qui: gen cycle_start_date = idtime*peak
qui: gen trough_val       = ((1-${transform})*cycle_linear_${var_y}`n' + ${transform}*cycle_hp_${var_y}`n')*trough	/* dating on aggregate or on cycle */
qui: gen cycle_start_val  = ((1-${transform})*cycle_linear_${var_y}`n' + ${transform}*cycle_hp_${var_y}`n')*peak
qui: collapse (mean) trough_dat trough_val cycle_start_dat cycle_start_val, by(id_lbc)
qui: gen amr = `n'
* Rearrange ordering of local business cycles:
if `n'==1{
qui: gen ongoing_rec = 0
qui: replace ongoing_rec = 1 if id_lbc == 0
qui: by amr, sort: egen flag = sum(ongoing_rec)
qui: replace id_lbc = id_lbc+1 if flag == 1
qui: drop flag
qui: save localcycles${version}.dta, replace
}
else{
qui: gen ongoing_rec = 0
qui: replace ongoing_rec = 1 if id_lbc == 0
qui: by amr, sort: egen flag = sum(ongoing_rec)
qui: replace id_lbc = id_lbc+1 if flag == 1
qui: drop flag
qui: append using localcycles${version}.dta
qui: save localcycles${version}.dta, replace
}
}
qui: gen idtime = cycle_start_dat
merge 1:1 idtime amr using cyclesync${version}.dta
drop _merge
qui: replace cyclesync = 0 if cyclesync == .
qui: xtset amr id_lbc
qui: gen cycle_end_date = f.cycle_start_date
qui: gen cycle_end_val  = f.cycle_start_val
qui: gen length_reces = abs(trough_date - cycle_start_date)
qui: gen depth_reces  = abs(trough_val - cycle_start_val)
qui: gen length_recov = abs(cycle_end_date - trough_date)
qui: gen depth_recov  = abs(cycle_end_val - trough_val)
qui: gen degree_reces=abs(depth_reces/length_reces)
qui: gen degree_recov=abs(depth_recov/length_recov)

sum

save localcycles${version}.dta, replace
********************************************************************************
